package com.example.brazil_project.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.database.Cursor;
import android.util.Log;

import com.example.brazil_project.parameter.Parameter;
/**
 * 获取到货信息
 * @author charlie1
 *
 */
public class ArriveMsgUtil{

	
	public void getArriveMsg(){
		try {
			
		
			Activity Contxt = Parameter.openingContx;
			List list1 = new ArrayList();
			list1.add("username");
			Map <String,String> map1 =  SharedPreferencesUtil.getSharedPreferencesData( Contxt, "u_p", list1) ;
			String  username = map1.get("username");
			String First_Time = Parameter.getFirst_Time();
		
			//没有登录则不跑后台线程
				if(username==null||username.length()<=0){			
					return;
				}
				
				 List<NameValuePair> params = new ArrayList<NameValuePair>();  
			     params.add(new BasicNameValuePair("create_date",First_Time));  
			     params.add(new BasicNameValuePair("username",username)); 
			     
			     String selectsql ="";	
			     String result = HttpUtil.doPost(Parameter.getServerPath()+"APP/msg/getArrive.nut",params);
				 List<Map<String, Object>>list = jsonUtil.getlistForJson(result);
				
				 String sysmsg =null;
				 String yjfkmsg =null;
				 String tg3 =null;
				 String cx4 =null;
				 if(list!=null&&list.size()>0){
					 for (Map map : list) {
						  String Sproduct =   map.get("product").toString();
						  List<Map<String, Object>>product =jsonUtil.getlistForJson(Sproduct);
						 selectsql = "SELECT * FROM ArriveInform_table WHERE id = "+map.get("id")+" AND username ='"+username+"'";
						 Cursor c3 = SQLiteDatabaseUtil.select(selectsql);
						 //如果这条系统系统信息已经传给这位客户了，就不需要再传给他们
						 if(c3!=null&&c3.getCount()>0){
							 
							 continue;
						 }
						 
						 String insertSql = "INSERT INTO ArriveInform_table VALUES ("+map.get("id")+",'"+map.get("title")+"','"+map.get("content")+"','"+map.get("CreateDate")+"'"
						 		+ ",'"+username+"','"+0+"','"+map.get("msgType")+"','"+map.get("sender")+"','"+map.get("ioc")+"')";
						SQLiteDatabaseUtil.insert(insertSql);
						 selectsql = "SELECT * FROM ArriveInform_table WHERE id = "+map.get("id")+" AND username ='"+username+"'";
						
						 
						
						//添加产品关联表
						 for (Map productmap : product) {
							 selectsql = "SELECT * FROM ArriveProduct_table WHERE msg_id = "+map.get("id"); 
							 Cursor c23 = SQLiteDatabaseUtil.select(selectsql);
							 if(c3!=null&&c3.getCount()>0){
								continue;
							 }
							 insertSql = "INSERT INTO ArriveProduct_table VALUES ("+map.get("id")+",'"+productmap.get("name")+"','"+productmap.get("imgurl")+"','"+productmap.get("price")+"'"
									+ ",'"+productmap.get("specifications")+"','"+productmap.get("type")+"',"+productmap.get("id")+",'"+productmap.get("Stock")+"')";
							 
							 SQLiteDatabaseUtil.insert(insertSql);
						 }	
						 //如果有系统消息提示，一次多条也只发一次信息
						 if(sysmsg ==null&&map.get("msgType").toString().equals("1")){ //到空运
							 SendMsgUtil.sengNotification(Contxt, "到空运", "到空运", null,"assets/img/i_a.png",1);
							 sysmsg = "sent";
						 }
						 if(yjfkmsg ==null&&map.get("msgType").toString().equals("2")){//到新柜
							 SendMsgUtil.sengNotification( Contxt, "到新柜", "到新柜", null,"assets/img/msg_d.png",1);
							 yjfkmsg = "sent";
						 }
						 if(tg3 ==null&&map.get("msgType").toString().equals("3")){//推广信息
							 SendMsgUtil.sengNotification( Contxt, "推广信息", "推广信息", null,"assets/img/msg_t.png",1);
							 tg3 = "sent";
						 }
						 if(cx4 ==null&&map.get("msgType").toString().equals("4")){//促销信息
							 SendMsgUtil.sengNotification( Contxt, "促销信息", "促销信息", null,"assets/img/msg_c.png",1);
							 cx4 = "sent";
						 }
						 //修改服务器信息为已读
						 HttpUtil.doPost(Parameter.getServerPath()+"APP/msg/readmsg.nut?id="+map.get("id"));
						 }

				 }
				 Log.e("sysmsg", "sysmsg");
				
		} catch (Exception e) {
			// TODO: handle exception
		}
			
		
	
	}
}
